package cn.commonblog.common.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * 数据表基础字段
 */
@Data
@EqualsAndHashCode()
@Accessors(chain = true)
public class BaseEntity implements Serializable {

    /**
     * 删除标志
     */
    //1 已删除
    public static final Integer DELETE_FLAG_TRUE = 1;
    //0 未删除
    public static final Integer DELETE_FLAG_FALSE = 0;

    @TableId(value = "id", type = IdType.AUTO)
    @TableField("id")
    public Long id;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(value = "date_created", fill = FieldFill.INSERT)
    public Date dateCreated;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(value = "last_updated", fill = FieldFill.INSERT_UPDATE)
    public Date lastUpdated;

    /**
     * 逻辑删除标志  1为已删除，0为未删除
     */
    @TableLogic
    @TableField(value = "delete_flag", fill = FieldFill.INSERT)
    public Integer deleteFlag;

    /**
     * 数据版本号，用于乐观锁，insert后为1，update后自增
     *
     * TODO 需要注意：
     * TODO 1.只有mybatis-plus方法生效，自行写sql无效
     * TODO 2.updateById的Entity参数必须是从数据库里查出来的，这样更新后才会把version设置为newVersion
     */
    @Version
    @TableField(value = "version", fill = FieldFill.INSERT)
    public Integer version;

}
